package com.crack.nga2.volley;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.crack.nga2.entity.Floor;
import com.crack.nga2.entity.Reply;
import com.crack.nga2.entity.Theme;
import com.crack.nga2.entity.data.ThemeData;
import com.crack.nga2.utils.DataUtil;
import com.crack.nga2.utils.GlobalConst;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询用户回复记录
 * Created by Administrator on 2016/1/6.
 */
public class UserReplyRequest extends Request<List<Reply>> {
    private Response.Listener<List<Reply>> listener;

    public UserReplyRequest(int method, String url, Response.ErrorListener errorListener, Response.Listener<List<Reply>> listener) {
        super(method, url, errorListener);
        this.listener = listener;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headFields = new HashMap<>();
        headFields.put("Cookie", GlobalConst.Cookie);
        return headFields;
    }

    @Override
    protected Response<List<Reply>> parseNetworkResponse(NetworkResponse response) {
        String json;
        try {
            json = new String(response.data, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new VolleyError(e.getMessage()));
        }
        json = DataUtil.getData(json);
        json = json.replaceAll("(?<=\"attachs\":)(\"\"?)(?=,)", "{}");                  //去除引起解析错误的数据
        json = json.replaceAll("\"topped_topic\":\"\"", "\"topped_topic\":\"-1\"");     //...
        json = json.replaceAll("\"sub_forums\":\"\"", "\"sub_forums\":{}");             //...
        ThemeData data = new Gson().fromJson(json, ThemeData.class);
        Map<Integer, Theme> themeMap = data.get__T();
        List<Reply> list = new ArrayList<>();
        for (Map.Entry<Integer, Theme> entry : themeMap.entrySet()) {
            Theme theme = entry.getValue();
            Floor floor = theme.get__P();
            Log.i("userReply", floor + "");
            Reply reply = new Reply();
            reply.tid = floor.getTid();
            reply.subject = theme.getSubject();
            reply.content = floor.getContent();
            reply.lastReplier = theme.getLastposter();
            reply.lastReplyTime = theme.getLastpost();
            reply.replies = theme.getReplies();
            list.add(reply);
        }
        return Response.success(list, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(List<Reply> response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }
}